<template>
  <div class="page-classifyList">
    <filter-view @update-list="() => getRoleList()" />
    <a-table
      id="classifyTable"
      :row-selection="{ selectedRowKeys: statusState.selectedRowKeys, checkStrictly: false, onChange: onSelectChange }"
      :pagination="false"
      :expandRowByClick="false"
      row-key="role_id"
      :columns="columns"
      :data-source="state.roleList"
      :loading="state.loading"
      :scroll="{ x: 1500 }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'role_video_name'">
          <a-tooltip placement="top" color="#fff" v-if="record.role_video_name">
            <template #title>
              {{ record.role_video_name }}
            </template>
            <div class="ellipsis-text">{{ record.role_video_name }}</div>
          </a-tooltip>
          <span v-else>-</span>
        </template>
        <template v-if="column.key === 'role_name'">
          <a-tooltip placement="top" color="#fff" v-if="record.role_name">
            <template #title>
              {{ record.role_name }}
            </template>
            <div class="ellipsis-text">{{ record.role_name }}</div>
          </a-tooltip>
          <span v-else>-</span>
        </template>
        <template v-if="column.key === 'role_person_name'">
          <a-tooltip placement="top" color="#fff" v-if="record.role_person_name">
            <template #title>
              {{ record.role_person_name }}
            </template>
            <div class="ellipsis-text">{{ record.role_person_name }}</div>
          </a-tooltip>
          <span v-else>-</span>
        </template>
        <template v-else-if="column.key === 'role_popularity'">
          <span>
            {{ formatNumber(record.role_popularity) }}
          </span>
        </template>
        <template v-else-if="column.key === 'created_at'">
          <span>
            {{ formatToDateTime(+record.created_at * 1000) }}
          </span>
        </template>
        <template v-else-if="column.key === 'role_status'">
          <span>
            <a-switch
              v-model:checked="record.role_status"
              :checkedValue="1"
              :unCheckedValue="2"
              @change="(checked:any) => editStatus(checked, record)"
            />
          </span>
        </template>
        <template v-else-if="column.key === 'action'">
          <span>
            <a class="basicColor" @click="updateRole(record)">编辑</a>
            <a-divider type="vertical" />
            <a class="basicColor" @click="deleteRole(record)"> 删除 </a>
          </span>
        </template>
      </template>
    </a-table>
    <a-pagination
      show-size-changer
      :defaultPageSize="10"
      :pageSizeOptions="['10', '20', '50', '100']"
      :show-total="(total:number)=>`共${total}条`"
      :total="queryState.total"
      @change="handlePaginationChange"
    />
    <!-- 删除对话框 -->
    <modal-view
      title="提示"
      v-model:visible="deleteVisible"
      okText="确定"
      cancelText="取消"
      :width="458"
      @confirm="deleteConfirm"
      @cancel="deleteCancel"
    >
      <span>确定要删除该角色吗</span>
    </modal-view>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
import { message } from 'ant-design-vue'
import { formatNumber, formatToDateTime } from '@/utils/format'
import FilterView from './components/FilterView.vue'
import { column, roleData } from './dataSource'
import type { RoleListData } from '@/types/site/role'
import ModalView from '@/components/components/modal/ModalView.vue'
import { roleState, statusState } from './state'
import router from '@/router'
import apis from '@/apis'
import { storage } from '@/utils/storage'
const {
  site: { role_list, role_detail, role_add, role_update, role_del, edit_status }
} = apis
const columns = ref(column)
const deleteVisible = ref(false)
const state = reactive<{ loading: boolean; roleList: RoleListData[] }>({
  loading: false,
  roleList: [] as RoleListData[]
})
const siteId = storage.get('siteId')
type Key = string
const deleteId = ref()
const queryState = reactive({
  offset: 0, // 默认当前页数
  limit: 10, // 默认当前页显示数据的大小
  total: 0 // 总数，必须先有
})
/**
 * 分页改变事件
 */
const handlePaginationChange = (size: number, limit: number) => {
  queryState.limit = limit
  queryState.offset = (size - 1) * limit
  getRoleList()
}
//获取影人列表接口
const getRoleList = () => {
  state.loading = true
  console.log('getActiveList')
  // 更新列表接口
  const params = {
    ...roleState,
    ...queryState
  }
  if (!params.role_id) params.role_id = undefined
  params.site_id = siteId
  role_list(params)
    .then((res: any) => {
      console.log('角色列表: ', res)
      state.roleList = res?.roles ?? []
      queryState.total = +res?.total ?? 0
      state.loading = false
      //处理下一页，第一条删除问题
      if (queryState.offset >= (res?.roles ?? 0) && queryState.total != 0) {
        queryState.offset -= queryState.limit
        getRoleList()
      }
    })
    .catch(() => {
      state.loading = false
    })
}
const onSelectChange = (rowKeys: Key[]) => {
  console.log('selectedRowKeys changed: ', rowKeys)
  statusState.selectedRowKeys = rowKeys
}
const deleteRole = (item: RoleListData) => {
  console.log('item: ', item)
  deleteVisible.value = true
  deleteId.value = item.role_id
}
//编辑
const updateRole = (item: RoleListData) => {
  console.log('item: ', item)
  router.push({ name: 'EditRole', query: { id: item.role_id } })
}
const editStatus = (checked: number, row: RoleListData) => {
  const data = {
    type: 11,
    status: checked,
    id: [row.role_id]
  }
  edit_status(data).then(() => {
    message.success(checked === 1 ? '启用成功' : '禁用成功')
    getRoleList()
  })
}
/**
  删除分类对话框事件
 */
const deleteConfirm = (value: any) => {
  console.log('value: ', value)
  role_del({ role_id: [deleteId.value] }).then(() => {
    message.success('删除角色成功')
    deleteVisible.value = value
    getRoleList()
  })
}
const deleteCancel = () => {
  deleteVisible.value = false
}
onMounted(() => {
  getRoleList()
})
</script>
<style lang="less" scoped>
@basicColor: #0083ff;

:deep(.ant-form-item-label) {
  width: 80px;
  text-align: right;
}

:deep(.ant-form-item-label > label::after) {
  content: '';
}

.page-classifyList {
  padding-top: 6px;
  box-sizing: border-box;

  :deep(.ant-table-thead > tr > th) {
    color: #262626;
    font-weight: 500;
  }

  :deep(.ant-table-thead
      > tr
      > th:not(:last-child):not(.ant-table-selection-column):not(.ant-table-row-expand-icon-cell):not([colspan])::before) {
    width: 0;
  }

  :deep(.ant-table-cell .ant-table-cell-with-append) {
    display: flex;
    align-items: center;
  }

  :deep(.ellipsis-text) {
    max-width: 154px !important;
    white-space: nowrap;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    cursor: pointer;
  }

  .basicColor {
    color: @basicColor;
  }
}
</style>
